/**
 * 
 */
package com.testing.mq.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 *
 * @author Jason
 * created at: Sep 26, 2011
 */
public abstract class AbstractSimpleMQWorker {
	
	public static final String Q_NAME="jasonTestQueue";
	public static final String Q_HOST="localhost";
	
	String qName;
	String qHost;
	Channel qChannel;
	Connection qConnection;
	
	/**
	 * ctor.
	 * use all default values... 
	 * @see #Q_NAME
	 * @see #Q_HOST
	 */
	public AbstractSimpleMQWorker() {
		this(Q_HOST, Q_NAME);
	}
	/**
	 * ctor.
	 * @param pQHost
	 * @param pQName
	 */
	public AbstractSimpleMQWorker(String pQHost, String pQName) {
		if (pQHost!=null && !pQHost.isEmpty()) {
			this.qHost=pQHost;
		} else {
			this.qHost=Q_HOST;
		}
		
		if (pQName!=null && !pQName.isEmpty()) {
			this.qName=pQName;
		} else {
			this.qName=Q_NAME;
		}
		this.init();
	}
	
	
	/**
	 * 
	 */
	protected void init() {
		try {
			ConnectionFactory aCF=new ConnectionFactory();
			
			aCF.setHost(this.qHost);
			qConnection=aCF.newConnection();
			this.qChannel=qConnection.createChannel();
			/*
			 * if you run queueDeclare --> you are creating a Queue
			 */
			//this.qChannel.queueDeclare(this.qName, false, false, false, null);
			
		} catch(Exception e) {
			e.printStackTrace();
			throw new RuntimeException("[AbstractSimpleMQWorker][init]", e);
		}
	}
	
	/**
	 * clean up method
	 */
	public abstract void dispose();
}
